package com.egene.tw.activity.contact;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.EditText;

import com.egene.tw.R;
import com.egene.tw.api.ContactApi;
import com.egene.tw.base.BaseFragment;
import com.egene.tw.dao.UserDao;
import com.egene.tw.model.LeaveMsg;
import com.egene.tw.model.net.ResponseBase;
import com.egene.tw.model.net.ResponseList;
import com.egene.tw.utils.JsonUtil;
import com.egene.tw.utils.MToast;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import java.util.List;


@EFragment(R.layout.fragment_leave_message)
public class LeaveMessageFragment extends BaseFragment {
    @ViewById
    EditText etMsg;
    @ViewById
    RecyclerView recycleView;

    @Bean
    UserDao userDao;

    LinearLayoutManager mLayoutManager;
    LeaveMessageAdapter mAdapter;

    @AfterViews
    void initViews(){

        mLayoutManager = new LinearLayoutManager(getActivity());
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(mLayoutManager);

        mAdapter = new LeaveMessageAdapter();
        recycleView.setAdapter(mAdapter);

        fetchList();
    }

    @Click(R.id.btnSend)
    void send(){
        String content = etMsg.getText().toString();
        if(TextUtils.isEmpty(content)){
            MToast.show(R.string.please_enter_message);
            return;
        }
        showWaitDialog(R.string.send).show();

//        EUser user =userDao.getUser();
        ContactApi.sendMessage(userDao.getUser().getUserID(), content, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                ResponseBase result =JsonUtil.parse(responseBody, ResponseBase.class);
                if(result != null){
                    etMsg.setText("");
                    fetchList();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                MToast.show(R.string.no_network);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                hideWaitDialog();
            }
        });


    }


    void fetchList() {
//        userDao.getUser().getUserID()
        ContactApi.getMessageList(userDao.getUser().getUserID(), new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                ResponseList<LeaveMsg> response = JsonUtil.toList(responseBody,
                        new TypeToken<ResponseList<LeaveMsg>>() {
                        }.getType());
                if (response != null) {
                    updateList(response.getData());
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                MToast.show(R.string.no_network);
            }
        });
    }

    private void updateList(List<LeaveMsg> data) {
        if(recycleView == null){
            return;
        }
        if(data != null){
            mAdapter.clear();
            mAdapter.addData(data);
            recycleView.scrollToPosition(mAdapter.getItemCount() - 1);
        }
    }

}
